package com.lijian.mapper;

import com.lijian.model.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from sys_user u order by u.id limit #{page}, #{limit}")
    List<SysUser> findAllUsersByPage(@Param("page") Integer page, @Param("limit") Integer limit);

    @Select("select count(*) from sys_user u")
    Long countAllUsers();

    @Options(useGeneratedKeys = true, keyProperty = "id")//id自增长
    @Insert("insert into sys_user(username, password, nickname, headImgUrl, phone, telephone, email, birthday, sex, status, createTime, updateTime) values(#{username}, #{password}, #{nickname}, #{headImgUrl}, #{phone}, #{telephone}, #{email}, #{birthday}, #{sex}, #{status}, #{createTime}, #{updateTime})")
    void save(SysUser user);

    @Select("select * from sys_user u where u.telephone = #{telephone}")
    SysUser findUserByPhone(String telephone);

    @Select("select * from sys_user u where u.username = #{username}")
    SysUser findUserByUsername(String username);

    @Select("select * from sys_user u where u.id = #{id}")
    SysUser findUserById(Long id);

    void update(SysUser sysUser);

    @Delete("delete from sys_user where id = #{id}")
    void deleteById(Integer id);

    @Select("select count(*) from sys_user u where u.username like '%${username}%'")
    Long findUserCountBySearch(@Param("username") String username);

    @Select("select * from sys_user u where u.username like '%${username}%' limit #{page}, #{limit}")
    List<SysUser> findUserPageBySearch(@Param("username") String username,@Param("page") Integer offset,@Param("limit") Integer limit);

    @Update("update sys_user set password = #{password} where id = #{id}")
    void changePassword(@Param("id") Integer id, @Param("password") String newPassword);
}
